<template>
  <div class="home-container">
    <header class="header">
      <div class="logo">系统管理平台</div>
      <div class="user-info">
        <span>欢迎，{{ userStore.userInfo.username || '用户' }}</span>
        <button @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
    </header>

    <main class="main-content">
      <aside class="sidebar">
        <ul class="menu">
          <li class="menu-item">
            <span class="icon">📊</span>
            <router-link to="/dashboard" class="link">仪表盘</router-link>
          </li>
          <li class="menu-item">
            <span class="icon">👥</span>
            <router-link to="/user-manage" class="link">用户管理</router-link>
          </li>
        </ul>
      </aside>

      <section class="content">
        <h2>首页</h2>
        <div class="stats-card">
          <p>当前用户：{{ userStore.userInfo.username || '未登录' }}</p>
          <p>登录时间：{{ formatTime(userStore.loginTime) }}</p>
        </div>
      </section>
    </main>
  </div>
</template>

<script>
import { onMounted } from 'vue';
import { useUserStore } from '@/stores/user';
import { useRouter } from 'vue-router';

export default {
  setup() {
    const userStore = useUserStore();
    const router = useRouter();

    onMounted(() => {
      if (!userStore.token) router.push('/login');
    });

    const handleLogout = () => {
      userStore.logout();
      router.push('/login');
    };

    const formatTime = (time) => {
      return time ? new Date(time).toLocaleString() : '未知';
    };

    return { userStore, handleLogout, formatTime };
  }
};
</script>

<style scoped>
.home-container { min-height: 100vh; display: flex; flex-direction: column; }
.header { height: 60px; background: #1e88e5; color: white; display: flex; justify-content: space-between; padding: 0 20px; align-items: center; }
.logo { font-weight: bold; }
.user-info { display: flex; gap: 15px; }
.logout-btn { background: #ff4081; border: none; color: white; padding: 5px 12px; border-radius: 4px; cursor: pointer; }
.main-content { display: flex; flex: 1; }
.sidebar { width: 200px; background: white; border-right: 1px solid #eee; }
.menu { list-style: none; padding: 0; }
.menu-item { padding: 12px 20px; }
.link { text-decoration: none; color: #333; display: flex; align-items: center; gap: 8px; }
.link.router-link-exact-active { color: #1e88e5; font-weight: bold; }
.content { flex: 1; padding: 20px; background: #f5f7fa; }
.stats-card { background: white; padding: 20px; border-radius: 8px; margin-top: 15px; }
</style>